package com.qfedu.provider.aigc;

import com.zhipu.oapi.ClientV4;
import com.zhipu.oapi.Constants;
import com.zhipu.oapi.service.v4.model.ChatCompletionRequest;
import com.zhipu.oapi.service.v4.model.ChatMessage;
import com.zhipu.oapi.service.v4.model.ChatMessageRole;
import com.zhipu.oapi.service.v4.model.ModelApiResponse;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * 千锋Java,为你而生！
 *
 * @Author: Feri
 * @Date:2024/11/21 14:50
 * @Modified:
 */
@Slf4j
public class AigcUtil {
    private static final String KEY="78930c62b0dd2361ca82c396ddfc3775.djakl1v4HgHmk3lE";
    private static ClientV4 client;
    static {
        client = new ClientV4.Builder(KEY).build();
    }
    /**
     * 发起请求*/
    public static String createQuestion(String role,String question){
        List<ChatMessage> messages = new ArrayList<>();
        ChatMessage msg1 = new ChatMessage(ChatMessageRole.SYSTEM.value(), role);
        ChatMessage msg2 = new ChatMessage(ChatMessageRole.USER.value(), question);
        messages.add(msg1);
        messages.add(msg2);
        String requestId = System.currentTimeMillis()+"";

        ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
                .model(Constants.ModelChatGLM4)
                .stream(Boolean.FALSE)
                .invokeMethod(Constants.invokeMethod)
                .messages(messages)
                .requestId(requestId)
                .build();
        ModelApiResponse response = client.invokeModelApi(chatCompletionRequest);
        if(response.isSuccess()) {
            log.info("AIGC审核动态-{}",response.toString());
            return response.getData().getChoices().get(0).getMessage().getContent().toString();
        }
        return null;
    }
}
